package main

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
)

func GetFiles(folder string) []string {
	files, _ := ioutil.ReadDir(folder)
	var fileName []string
	for _, file := range files {
		if file.IsDir() {
			// fmt.Println(folder + "/" + file.Name())
			fileName = append(fileName, file.Name())
			// GetFiles(folder + "/" + file.Name())   //递归
		} else {
			// fmt.Println(folder + "/" + file.Name())
			fileName = append(fileName, file.Name())
		}
	}
	return fileName
}

func insertAccount() { //data要自带"\n"
	var accountName string
	fmt.Println("请输入远程主机的用户名:")
	fmt.Scanln(&accountName)
	var hostIp string
	fmt.Println("请输入远程主机的IP:")
	fmt.Scanln(&hostIp)
	var password string
	fmt.Println("请输入远程主机的密码:")
	fmt.Scanln(&password)
	var data string
	data = accountName + "@" + hostIp + " " + password + "\n"

	file, err := os.OpenFile("account.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) //必须控制文件类型
	if err != nil {
		fmt.Println("打开account.log失败")
	}
	writer := bufio.NewWriter(file)
	writer.WriteString(data)
	writer.Flush()
	fmt.Println(data + "insert successful!")
	defer file.Close()
}

func readAccount() []string {
	file, err := os.Open("account.log")
	if err != nil {
		fmt.Printf("打开文件失败，err:%v\n", err)
		return nil
	}

	reader := bufio.NewReader(file)

	var account []string

	for i := 1; ; i++ {
		line, errReader := reader.ReadString('\n')
		if errReader == io.EOF {
			fmt.Println("读取完毕！")
			break
		}
		if errReader != nil {
			fmt.Printf("读取文件失败！err:%v\n", err)
			return nil
		}
		fmt.Printf("%d:%s\n", i, line)
		account = append(account, line)
	}
	defer file.Close()
	return account
}

func scp() {
	fmt.Println("start:")
	fileName := GetFiles("./conveyFile")
	fmt.Println("scp status:")
	var fileParameter string
	for _, file := range fileName {
		fmt.Println("ready scp:" + file)
		fileParameter += file + " "
	}
	fmt.Println("ready parameter:" + fileParameter)

	fmt.Println("please select account:")
	account := readAccount()
	// fmt.Println("返回的account:" + account[0])
	var choice int
	fmt.Println("请输入要传送的主机:")
	fmt.Scanf("%d", &choice)
	chosenAccount := account[choice-1]
	spaceIndex := strings.Index(chosenAccount, " ")
	hostIP := chosenAccount[0:spaceIndex]
	// password := chosenAccount[spaceIndex+1:]
	var destination string
	fmt.Println("请输入目的主机目录:")
	fmt.Scanln(&destination)
	var cmd string

	co := "./scp.sh " + hostIP + ":" + destination
	fmt.Println(co)
	fmt.Println(cmd)

	shell := exec.Command("/bin/sh", "-c", co)
	fmt.Println(shell.Args)
	stdout, err := shell.StdoutPipe()
	shell.Stdin = os.Stdin
	if err != nil {
		fmt.Println(err)
		return
	}
	shell.Run()
	reader := bufio.NewReader(stdout)
	for {
		line, err2 := reader.ReadString('\n')
		if err2 != nil || io.EOF == err2 {
			break
		}
		fmt.Println(line)
	}
	shell.Wait()
}

func main() {
	for {
		fmt.Println("menu:")
		fmt.Println("1.scp")
		fmt.Println("2.insert account")
		fmt.Println("0.Exit")
		var choice int
		fmt.Scanf("%d", &choice)
		if choice == 1 {
			scp()
		} else if choice == 2 {
			insertAccount()
		} else if choice == 0 {
			os.Exit(0)
		} else {
			fmt.Println("您的输入有误,请重新输入！")
		}
	}
}
